package 山脉数组的峰顶索引852;

/**
 * @author 谢阳
 * @version 1.8.0_131
 */

class Solution {
    public static void main(String[] args) {
        int arr[] = {24, 69, 100, 99, 79, 78, 67, 36, 26, 19};
        int i = peakIndexInMountainArray(arr);
        System.out.println(i);
    }

    public static int peakIndexInMountainArray(int[] arr) {
        //int max = arr[0];
        //int index = 0;
        //for (int i = 1; i < arr.length; i++) {
        //    if (max < arr[i]){
        //        max = arr[i];
        //        index = i;
        //    } else {
        //        break;
        //    }
        //}
        //return index;

        //int l = 1;
        //int r = arr.length - 2;
        //int mid;
        //int ans = 0;
        //while (l <= r) {
        //    mid = (l + r) / 2;
        //    if (arr[mid] > arr[mid + 1]) {
        //        ans = mid;
        //        l = mid - 1;
        //    } else {
        //        r = mid + 1;
        //    }
        //}
        //return ans;

        int n = arr.length;
        int left = 1, right = n - 2, ans = 0;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] > arr[mid + 1]) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;


    }
}